﻿#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace game_for_test_helper.src.graphics
{
    class SimpleCamera : Microsoft.Xna.Framework.GameComponent
    {
        #region 字段
        private static Matrix viewMatrix;
        //摄像机的初始坐标
        private Vector3 cameraPostion;
        private Vector3 CameraTarget;

        #endregion

        #region 构造函数
        public SimpleCamera(Game game)
            : base(game)
        {
        } // SimpleCamera(game)
        #endregion

        #region 初始化
        public override void Initialize()
        {
            base.Initialize();
            cameraPostion = new Vector3(-500,500,0);
            CameraTarget = Vector3.Zero;
            this.Game.GraphicsDevice.RenderState.AlphaBlendEnable = true;
            this.Game.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
            this.Game.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
            this.Game.GraphicsDevice.RenderState.DepthBufferEnable = true;
        } // Initialize
        #endregion

        #region Update
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            Vector2 RightPad = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right;

            if (GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
                cameraPostion.Y -= 5;
            if (GamePad.GetState(PlayerIndex.One).DPad.Down == ButtonState.Pressed)
                cameraPostion.Y += 5;
            if (CameraFollowObject!=null)
            {
                CameraTarget = CameraFollowObject.TargetPosition;
                cameraPostion.X=CameraTarget.X+-500;
                cameraPostion.Z = CameraTarget.Z;
            }
            //cameraPostion.X += RightPad.X*10;
            //cameraPostion.Z += RightPad.Y*10;
            ViewMatrix = Matrix.CreateLookAt(
               cameraPostion, CameraTarget , Vector3.Up);
        }
        #endregion

        #region viewMatrix
        public static Microsoft.Xna.Framework.Matrix ViewMatrix
        {
            get { return viewMatrix; }
            set { viewMatrix = value; }
        }

        public Vector3 CameraPostion
        {
            get { return cameraPostion; }
            set { cameraPostion = value; }
        }

        public static void SetFollowObject(ICameraFollowObject Fobject)
        {
            CameraFollowObject = Fobject;
        }
        private static ICameraFollowObject CameraFollowObject;
        #endregion
    }
}
